package com.example.ymm.modules1.service.impl.hl.ex;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.ymm.base.BaseServiceImpl;
import com.example.ymm.exception.YmException;
import com.example.ymm.model.TableList;
import com.example.ymm.model.YmPageDto;
import com.example.ymm.modules1.entity.hl.ex.ExTeam;
import com.example.ymm.modules1.entity.hl.ex.ExTeamRule;
import com.example.ymm.modules1.mapper.ex.ExTeamRuleMapper;
import com.example.ymm.modules1.query.hl.ex.ExTeamRuleQueryCriteria;
import com.example.ymm.modules1.service.hl.ex.ExTeamRuleService;
import com.example.ymm.modules1.service.hl.ex.ExTeamService;
import com.example.ymm.util.QueryHelpPlus;
import com.example.ymm.util.UtilValidate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
@DS("slave2")
public class ExTeamRuleImpl extends BaseServiceImpl<ExTeamRuleMapper, ExTeamRule> implements ExTeamRuleService {

    @Autowired
    ExTeamService exTeamService;


    @Override
    public TableList<ExTeamRule> list(ExTeamRuleQueryCriteria criteria, YmPageDto pageDto) {
        IPage<ExTeamRule> page = new Page<ExTeamRule>(pageDto.getPage(),pageDto.getPageSize());
        QueryWrapper predicate = QueryHelpPlus.getPredicate(ExTeamRule.class, criteria);
//        predicate.orderByAsc("sort");
        baseMapper.selectPage(page, predicate);
        TableList<ExTeamRule> tableList=new TableList(page);
        return tableList;
    }

    public List<ExTeamRule> queryAll(ExTeamRuleQueryCriteria criteria) {
        QueryWrapper predicate = QueryHelpPlus.getPredicate(ExTeamRule.class, criteria);
//        predicate.orderByAsc("sort");
        List<ExTeamRule> result =baseMapper.selectList(predicate);
        return result;
    }


    @Override
    public void initRule() {
        List<ExTeam> teams = exTeamService.queryAll(null);
        for (ExTeam team : teams) {
            ExTeamRuleQueryCriteria c1=new ExTeamRuleQueryCriteria();
            c1.setTeamId(team.getTeamId());
            List<ExTeamRule> exTeamRules = queryAll(c1);
            if(UtilValidate.isEmpty(exTeamRules)){

                ExTeamRule rule1=new ExTeamRule();
                rule1.setTeamId(team.getTeamId());
                rule1.setTeamName(team.getName());
                rule1.setTrueScore(1);
                rule1.setErroScore(2);
                rule1.setType("本部门");
                saveOrUpdate(rule1);
                ExTeamRule rule2=new ExTeamRule();
                rule2.setTeamId(team.getTeamId());
                rule2.setTeamName(team.getName());
                rule2.setTrueScore(2);
                rule2.setErroScore(0);
                rule2.setType("非本部门");
                saveOrUpdate(rule2);
            }else{
                exTeamRules.forEach(exTeamRule -> {
                    if(exTeamRule.getType().equals("本部门")){
                        exTeamRule.setTrueScore(1);
                        exTeamRule.setErroScore(2);
                    }
                    if(exTeamRule.getType().equals("非本部门")){
                        exTeamRule.setTrueScore(2);
                        exTeamRule.setErroScore(0);
                    }
                    saveOrUpdate(exTeamRule);
                });

            }
        }
    }


    @Override
    public void saveTeamRules(ExTeam team) {
        if(UtilValidate.isEmpty(team.getTeamId())){
            throw new YmException("参数异常!");
        }
        ExTeam team1 = exTeamService.getById(team.getTeamId());

        LambdaQueryWrapper<ExTeamRule> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ExTeamRule::getTeamId, team.getTeamId());
        remove(queryWrapper);
        List<ExTeamRule> rules = team.getRules();
        for (ExTeamRule rule : rules) {
            rule.setTeamId(team1.getTeamId());
            rule.setTeamName(team1.getDeptName());
        }
        saveOrUpdateBatch(rules);
    }
}
